package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.map.MapUtil;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HaodeDataMap;
import com.ruoyi.system.service.IHaodeDataMapService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 地图数据Controller
 * 
 * @author ruoyi
 * @date 2020-09-11
 */
@Controller
@RequestMapping("/system/map")
public class HaodeDataMapController extends BaseController
{
    private String prefix = "system/map";

    @Autowired
    private IHaodeDataMapService haodeDataMapService;

    @RequiresPermissions("system:map:view")
    @GetMapping()
    public String map()
    {
        return prefix + "/map";
    }

    /**
     * 查询地图数据列表
     */
    @RequiresPermissions("system:map:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HaodeDataMap haodeDataMap)
    {
        startPage();
        List<HaodeDataMap> list = haodeDataMapService.selectHaodeDataMapList(haodeDataMap);
        return getDataTable(list);
    }

    /**
     * 导出地图数据列表
     */
    @RequiresPermissions("system:map:export")
    @Log(title = "地图数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HaodeDataMap haodeDataMap)
    {
        List<HaodeDataMap> list = haodeDataMapService.selectHaodeDataMapList(haodeDataMap);
        ExcelUtil<HaodeDataMap> util = new ExcelUtil<HaodeDataMap>(HaodeDataMap.class);
        return util.exportExcel(list, "map");
    }

    /**
     * 新增地图数据
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存地图数据
     */
    @RequiresPermissions("system:map:add")
    @Log(title = "地图数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HaodeDataMap haodeDataMap)
    {
        return toAjax(haodeDataMapService.insertHaodeDataMap(haodeDataMap));
    }

    /**
     * 修改地图数据
     */
    @GetMapping("/edit/{dataMapId}")
    public String edit(@PathVariable("dataMapId") Long dataMapId, ModelMap mmap)
    {
        HaodeDataMap haodeDataMap = haodeDataMapService.selectHaodeDataMapById(dataMapId);
        mmap.put("haodeDataMap", haodeDataMap);
        return prefix + "/edit";
    }

    /**
     * 修改保存地图数据
     */
    @RequiresPermissions("system:map:edit")
    @Log(title = "地图数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HaodeDataMap haodeDataMap)
    {
        if(!StringUtils.isBlank(haodeDataMap.getMapAddress())&&StringUtils.isBlank(haodeDataMap.getMapLnglat())){
            haodeDataMap.setMapLnglat( MapUtil.getLngLat(haodeDataMap.getMapAddress()));
        }
        return toAjax(haodeDataMapService.updateHaodeDataMap(haodeDataMap));
    }

    /**
     * 删除地图数据
     */
    @RequiresPermissions("system:map:remove")
    @Log(title = "地图数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(haodeDataMapService.deleteHaodeDataMapByIds(ids));
    }
}
